<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class a_newsletter extends CI_Controller {
    private $sessao = "";

    function __construct(){
        parent::__construct();
        $this->load->model("NewsletterModel");
        $this->load->helper("auth");
        $this->load->helper("mail");
        //$this->sessao = a_logado();
       
    }

      public function cadastrar(){

        $this->load->helper("form");
        $this->load->library("form_validation");

        $dados = array(
            "titulo" => "Cadastro no Newsletter",
            "user" => "Visitante",
            "erro" => ""
            );

        $main = array(
            "dados" => $dados,
            "link" => "all/a_cad_news_view",
            );

        //Validação dos campos
        $this->form_validation->set_rules('nome', 'Nome', 'required|min_length[]');
        $this->form_validation->set_rules('email', 'E-mail', 'required|valid_email|callback_email_check');

        if ($this->form_validation->run() == false) :
            $this->load->view('template', $main);

        else :
            $campos = array(
                "nome" => $this->input->post("nome"),
                "email" => $this->input->post("email"),
               
            );
            $this->NewsletterModel->inserir($campos);
            emailCadastroNewsLetter($this->input->post('email'));

            $this->Log->inserir(tipo_usuario(), id_tipo_usuario(), "se cadastrou", "newsletter", $idInserido);
            redirect("home/index/sucesso");
        //edirect("admin/cliente");
        endif;
    }

    function email_check($str){
        if ($this->NewsletterModel->getNewsletterEmail($str) != false) :
            $this->form_validation->set_message('email_check', 'Já há um cadastro com este e-mail.');
            return FALSE;

        else :
            return TRUE;

        endif;
    }
    
}